package com.lovo.addressbook;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;

import junit.framework.Assert;
import junit.framework.TestCase;

public class MainWindowTest extends TestCase {

	public void testMainWindow() {
		//fail("Not yet implemented");
	}

	MainWindow wm;
	User user;
	User [] users;
	protected void setUp() throws Exception {
		super.setUp();
		wm = new MainWindow();
		TestUtil.initialConfig();
		user = TestUtil.getUser();
		users = TestUtil.getUsers();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}
	
	public void testMain() {
		Component [] components = wm.getComponents();
		System.out.println(components.length);
		for (int i = 0; i != components.length; ++i) {
			System.out.println(components.getClass());
		}
		wm.btnNext.doClick();
		ActionEvent e = new ActionEvent(wm.btnNext, 0, "11");
		wm.actionPerformed(e);
	}
	
	private void setText(MainWindow mw, User user)
	{
		mw.txtAge.setText(user.getAge().toString());
		mw.txtName.setText(user.getName());
		mw.txtNum.setText(user.getNo());
		mw.txtPhotoAddr.setText(user.getImagePath());
		mw.txtTel.setText(user.getPhone());
	}

	public void testDisplayUserInfo() {
		
		MainWindow wm = new MainWindow();
		wm.displayUserInfo(user);
		TestUtil.assertMatch(wm, user);
	}

	public void testActionPerformed() {
		wm.btnFirst.doClick();
		TestUtil.assertMatch(wm, users[0]);
			for (int i = 0; i != users.length - 1; ++i) {
				wm.btnNext.doClick();
				TestUtil.assertMatch(wm, users[i + 1]);
			}
		wm.btnLast.doClick();
		TestUtil.assertMatch(wm, users[users.length - 1]);
		for (int i = users.length - 1; i != 0; --i) {
			wm.btnPrevious.doClick();
			TestUtil.assertMatch(wm, users[i - 1]);
		}
		wm.btnAdd.doClick();
		Assert.assertEquals(Integer.parseInt(wm.txtNum.getText()), users.length + 1);
		setText(wm, user);
		wm.btnSave.doClick();
		wm.btnLast.doClick();
		TestUtil.assertMatch(wm, user);
		/*Integer no = users.length + 1;
		newUser.setNo(no.toString());
		TestUtil.assertMatch(wm, newUser);*/
		wm.btnAbout.doClick();		
	}

}
